/**
 * Created by lelecat on 2017/3/23.
 */

var CONFIG_HEADER = "config_header";
var CONFIG_OFFICE = "config_office";
var CONFIG_SEAL = "config_seal";
var CONFIG_FONT = "config_font";
var CONFIG_TYPE = "config_type";

/**
 * 公文设置标量构造函数
 * @param name
 * @param type
 * @constructor
 */
function DocConfigType(name, type) {
	this.name = name;
	this.type = type;
	this.path = type + "/";

}

var HEADER = new DocConfigType("公文文头", "header");
var OFFICE = new DocConfigType("办公室名", "office");
var SEAL = new DocConfigType("公文印章", "seal");
var FONT = new DocConfigType("文件字", "font");
var TYPE = new DocConfigType("公文类型", "type");

// 公文设置表格的操作对象
var doc_config_table = {

	DOC_CONFIG_PATH: "../account/doc/config/",

	__table: null,
	__type: null,

	// 初始化公文设置表格
	initDocConfigTable: function(id, type) {

		this.__type = type;

		this.__table = $("#" + id);
		var table_body = $(
			"<thead>" +
			"<tr style='text-align: center'>" +
			"<th width='60%' id='doc_config_name'>" + type.name + "</th> " +
			"<th width='40%'>操作</th>" +
			"</tr>" +
			"</thead>" +
			"<tbody>" +
			"</tbody>"
		);
		this.__table.empty().append(table_body);
	},

	// 在表格中列出所有数据
	listData: function (data, idx_default, ids) {

		// 如果不是公文印章类型, ids 元素值改变为对应下标值
		if (this.__type !== SEAL) {
			for (var i = 0; i < data.length; i++) {
				ids[i] = i;
			}
		}

		// 找到 tbody 节点
		var table_tbody = this.__table.find("tbody");

		// 遍历所有数据, 完成表格
		for (var i = 0; i < data.length; i++) {

			// 组合操作按钮的 HTML 语句
			var opt_content = null;
			if (idx_default !== i) {
				opt_content = this.__makeSetDefaultButton(ids[i]);
			} else {
				opt_content = this.__makeTrueDefaultButton();
			}
			opt_content	+= (" " + this.__makeModifyButton(ids[i]));
			opt_content	+= (" " + this.__makeDeleteButton(data[i], ids[i]));

			var tr = $(
				"<tr>" +
					"<td>" + data[i] + "</td>" +
					"<td>" + opt_content +"</td>" +
				"</tr>"
			);
			table_tbody.append(tr);
		}
	},

	__makeTrueDefaultButton: function () {
		var button = '<button type="button" data-toggle="modal" class="btn_is_default">已设为默认</button>';
		return button;
	},

	__makeSetDefaultButton: function (indexOrId) {

		var button = '<button ' +
			'type="button" class="btn_set_default" data-toggle="modal" ' +
			'onclick="setDefault(' + indexOrId + ')">设置为默认</button>';
		return button;
	},

	__makeModifyButton: function(indexOrId) {

		var data_target = "#edit_block";
		var button = '<button type="button" data-toggle="modal" class="btn_modify" ' +
			'data-target="' + data_target + '" onclick="updModifyBlock(' + indexOrId + ')">编辑</button>';
		return button;
	},

	__makeDeleteButton: function(content, indexOrId) {

		var data_target = "#delete_block";
		var button = '<button type="button" data-toggle="modal" class="btn_delete" ' +
			'data-target="' + data_target + '" onclick="updDeleteBlock(\'' + content + '\',' + indexOrId + ')">删除</button>';
		return button;

	}
};